<?php

// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\data\model\ShopGoodsAttr;
/**
 * 系统商品规格管理
 * Class Specs
 * @package app\admin\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/02/15 18:12
 */
class Specs extends BasicAdmin
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'ShopAttr';

    /**
     * 商品属性列表
     */
    public function index()
    {
        $this->title = '商品属性列表';

        $db = Db::name($this->table)
                ->alias('a')
                ->join('__SHOP_CATES__ c','c.id=a.model_id')
                ->field('a.*,c.cname')
                ->order('a.id','asc');

        $data = parent::_list($db,true,false);
        $list = [];
        foreach ($data['list'] as $k => $v) {
            $list[$k] = $v;
            if(is_array(json_decode($v['a_value'])) !== false){
                $list[$k]['a_value'] = implode(',',json_decode($v['a_value']));
            }
        }
        $this->assign('title', $this->title);
        return $this->fetch('',['list' => $list,'page' => $data['page']]);
       
    }

     /**
     * 表单数据前缀方法
     * @param array $data
     */
    protected function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            // 获取商品分类信息
            $menus = parent::get_cates();
            if(!empty($data)){
                $str = '';
                $a_value = json_decode($data['a_value'],true);
                if(is_array($a_value) !== false){
                    foreach ($a_value as $v) {
                        $str .= $v."\n";
                    }
                }
                $data['a_value'] = rtrim($str,"\n");
            }
            $this->assign('menus', $menus);
        }else{
           if($data['type'] == 2){
                $a_value = explode("\n",$data['a_value']);
                $value = [];
                if(!empty($a_value)){
                    foreach ($a_value as $val) {
                        if(!empty($val)){
                            $value[] = trim($val,"\r");
                        }
                    }
                }
                $data['a_value'] = json_encode($value);
            }else{
                $data['a_value'] = '';
            }
        }
    }


    /**
     * 商品属性添加
     */
    public function add()
    {
        return $this->_form($this->table, 'form');
    }

    /**
     * 商品属性编辑
     */
    public function edit()
    {
        return $this->_form($this->table, 'form');
    }

   

    /**
     * 删除商品属性
     */
    public function del()
    {   
        $get = $this->request->get();
        // 查询属性是否使用
        $goods_attr = ShopGoodsAttr::get(['attr_id',$get['id']]);
        
        if(!empty($goods_attr)){
            $this->error("该属性已有商品使用，无法删除！");
        }

        if (DataService::update($this->table)) {
            $this->success("商品属性删除成功！", '');
        }
        $this->error("商品属性删除失败，请稍候再试！");
    }


}
